// compute cohort-year specific family income percentile
use ../processed/income_cln.dta, clear
merge m:1 CASEID_1979 using ../data/background_raw.dta
assert _merge==3

gen age = year - 1 - (1900+Q1_3_A_Y_1979)
keep if SAMPLE_ID_1979<=14 & age<=24 & netfi>=0 // civilian sample

local NN = _N
gen netfi_pct = .
qui forvalues i=1/`NN'{
	gen temp = (netfi<netfi[`i']) + 0.5*(netfi==netfi[`i']) if Q1_3_A_Y_1979==Q1_3_A_Y_1979[`i'] & age==age[`i']
	sum temp [aw=SAMPWEIGHT_1979]
	replace netfi_pct = r(mean) in `i'
	drop temp
}

tab age Q1_3_A_Y_1979, m
tab age Q1_3_A_Y_1979 [aw=SAMPWEIGHT_1979], sum(netfi) nost nof noo
tab age Q1_3_A_Y_1979 [aw=SAMPWEIGHT_1979], sum(netfi_pct) nost nof noo

keep CASEID_1979 year netfi_pct
save ../processed/income_pct.dta, replace

